//
//  MainController.m
//  shezhan
//
//  Created by admin on 16/8/11.
//  Copyright © 2016年 haoniu. All rights reserved.
//

#import "HNMainController.h"
#import "HNHomeController.h"
#import "HNNativeHomeController.h"
#import "HNServerController.h"
#import "HNMyController.h"
#import "HNReleaseView.h"
#import "ConversationListController.h"
#import "HNPublishProductController.h"
#import "HNOtherImageController.h"

#if DEMO_CALL == 1
@interface HNMainController () <UIWebViewDelegate,HNReleaseViewDelegate,UIAlertViewDelegate, EMCallManagerDelegate>
#else
@interface HNMainController () <UIWebViewDelegate,HNReleaseViewDelegate,UIAlertViewDelegate>
#endif
{
    HNHomeController * _home;
    HNNativeHomeController * _nativeHome;
    HNServerController * _server;
    HNReleaseView * releaseView;//发布弹出视图
    ConversationListController *_chatListVC;//消息列表
    HNMyController * _my;
}
@property (nonatomic,strong) UIView *tabBarView;

@end

@implementation HNMainController


-(UIView *)tabBarView{
    if (!_tabBarView) {
        NSArray * _tabBarTitle = [NSArray arrayWithObjects:@"首页",@"服务",@"发布",@"消息",@"我的", nil];
        _tabBarView = [[UIView alloc]initWithFrame:CGRectMake(0, mainScreenSize.height-50, mainScreenSize.width, 50)];
        [_tabBarView setBackgroundColor:[UIColor whiteColor]];
        UILabel * line = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, mainScreenSize.width, 0.5)];
        [line setBackgroundColor:[AppConfig colorWithHexString:@"#cacaca"]];
        [_tabBarView addSubview:line];
        
        for (int i = 0; i<_tabBarTitle.count; i++) {
            UIButton * tabbarButton = [[UIButton alloc]initWithFrame:CGRectMake(mainScreenSize.width/_tabBarTitle.count*i, 0, mainScreenSize.width/_tabBarTitle.count, 50)];
            tabbarButton.tag = 10000+i;
            UIImage * image = [UIImage imageNamed:[NSString stringWithFormat:@"icon_tabbar%d",i+1]];
            [tabbarButton setImage:image forState:UIControlStateNormal];
            [tabbarButton setImage:[UIImage imageNamed:[NSString stringWithFormat:@"icon_tabbar%d_select",i+1]] forState:UIControlStateSelected];
            [tabbarButton setImageEdgeInsets:UIEdgeInsetsMake(0, mainScreenSize.width/10-image.size.width/2, 20, mainScreenSize.width/10-image.size.width/2)];
            [tabbarButton addTarget:self action:@selector(tabbarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            
            [tabbarButton setTitle:_tabBarTitle[i] forState:UIControlStateNormal];
            [tabbarButton setTitleColor:[AppConfig colorWithHexString:@"#454545"] forState:UIControlStateNormal];
            [tabbarButton setTitleColor:[AppConfig colorWithHexString:@"#d15353"] forState:UIControlStateSelected];
            [tabbarButton.titleLabel setFont:[UIFont systemFontOfSize:12]];
            [tabbarButton setTitleEdgeInsets:UIEdgeInsetsMake(31, -image.size.width, 5, 0)];
            
            [tabbarButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
            [_tabBarView addSubview:tabbarButton];
        }

    }
    return  _tabBarView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    if ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0) {
        self.edgesForExtendedLayout=UIRectEdgeNone;
    }
    
    //清除cookies
    NSHTTPCookie *cookie;
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (cookie in [storage cookies])
    {
        [storage deleteCookie:cookie];
    }
    //清除UIWebView的缓存
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    
    NSURLCache * cache = [NSURLCache sharedURLCache];
    [cache removeAllCachedResponses];
    [cache setDiskCapacity:0];
    [cache setMemoryCapacity:0];
    
    [self initTabBar];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated{
    [self.navigationController setNavigationBarHidden:YES];
}
-(void)viewWillDisappear:(BOOL)animated{
    //[self.navigationController setNavigationBarHidden:NO];
}




#pragma -mark 初始化TabBar
-(void)initTabBar{
    [self.tabBar setHidden:YES];
    _home = [[HNHomeController alloc]init];
    _nativeHome = [[HNNativeHomeController alloc]init];
    _server = [[HNServerController alloc]init];
//    _chatListVC = [[ConversationListController alloc] initWithNibName:nil bundle:nil];
    _chatListVC = [[ConversationListController alloc] initWithNibName:nil bundle:nil];
    [_chatListVC.view setFrame:CGRectMake(0, 65, mainScreenSize.width, mainScreenSize.height-124)];
    [_chatListVC networkChanged:_connectionState];
    _my = [[HNMyController alloc]init];
    
    //转换界面
    //    NSArray * viewArray = @[_home,_server,_chatListVC,_my];
    NSArray * viewArray = @[_nativeHome,_server,_chatListVC,_my];
    self.viewControllers = viewArray;
    
    [self.view addSubview:self.tabBarView];
    UIButton *originBtn = [(UIButton *)self.view viewWithTag:10000];
    originBtn.selected = YES;
//    self.selectedIndex = 0;
    //获取通知中心单例对象
    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
    //添加当前类对象为一个观察者，name和object设置为nil，表示接收一切通知
    [center addObserver:self selector:@selector(mainNotification:) name:@"HNMainControllerNotification" object:nil];
}
#pragma -mark 获取通知
-(void)mainNotification:(NSNotification *)notification{
    NSLog(@"=====NSNotification:%@",notification.userInfo);
    NSDictionary * userInfo = notification.userInfo;
    BOOL tabbarShow = [[userInfo objectForKey:@"tabbarShow"] boolValue];
    [_tabBarView setHidden:!tabbarShow];
    
    NSString * controller = [userInfo objectForKey:@"controller"];
    if (tabbarShow) {
        CGRect rect = CGRectMake(0, 20, mainScreenSize.width, mainScreenSize.height-70);
        if ([controller isEqualToString:@"home"]) {
            [_home.webView setFrame:rect];
        }else if ([controller isEqualToString:@"server"]) {
            [_server.webView setFrame:rect];
        }else if ([controller isEqualToString:@"my"]) {
            [_my.webView setFrame:rect];
        }
        //[self.tabBar setFrame:CGRectMake(0, mainScreenSize.height-self.tabBar.frame.size.height, mainScreenSize.width, self.tabBar.frame.size.height)];
        NSLog(@"=====%d",(int)_my.view.frame.size.height);
    }else{
        CGRect rect = CGRectMake(0, 20, mainScreenSize.width, mainScreenSize.height-20);
        if ([controller isEqualToString:@"home"]) {
            [_home.webView setFrame:rect];
        }else if ([controller isEqualToString:@"server"]) {
            [_server.webView setFrame:rect];
        }else if ([controller isEqualToString:@"my"]) {
            [_my.webView setFrame:rect];
        }
        //[self.tabBar setFrame:CGRectMake(0, mainScreenSize.height, mainScreenSize.width, self.tabBar.frame.size.height)];
        NSLog(@"=====%d",(int)_my.view.frame.size.height);
    }
}
#pragma mark - 点击方法执行两次才能刷掉样式错误
-(void)tabbarButtonClick:(UIButton *)btn{
    NSLog(@"%ld",btn.tag);
    for (int i = 10000; i<10005; i++) {
        UIButton *myButton = (UIButton *)[self.view viewWithTag:(i)];
        [myButton setSelected:NO];
    }
    
    if (btn.tag==10000) {
        self.selectedIndex = 0;
        btn.selected = YES;
    }else if (btn.tag==10001) {
        self.selectedIndex = 1;
        btn.selected = YES;
    }else if (btn.tag==10002) {
        if (releaseView==nil) {
            releaseView = [HNReleaseView shareInstance];
            releaseView.delegate = self;
        }
        [releaseView show];
        btn.selected = YES;
    }else if (btn.tag==10003) {
        self.selectedIndex = 2;
        btn.selected = YES;
    }else if (btn.tag==10004) {
        btn.selected = YES;
        self.selectedIndex = 3;
    }
    
    for (int i = 10000; i<10005; i++) {
        UIButton *myButton = (UIButton *)[self.view viewWithTag:(i)];
        [myButton setSelected:NO];
    }
    
    if (btn.tag==10000) {
        self.selectedIndex = 0;
        btn.selected = YES;
    }else if (btn.tag==10001) {
        self.selectedIndex = 1;
        btn.selected = YES;
    }else if (btn.tag==10002) {
        if (releaseView==nil) {
            releaseView = [HNReleaseView shareInstance];
            releaseView.delegate = self;
        }
        [releaseView show];
        btn.selected = YES;
    }else if (btn.tag==10003) {
        self.selectedIndex = 2;
        btn.selected = YES;
    }else if (btn.tag==10004) {
        btn.selected = YES;
        self.selectedIndex = 3;
    }
    
}
#pragma -mark HNReleaseView Delegate
-(void)HNReleaseView:(HNReleaseView *)releaseView didSelectButton:(UIButton *)btn{
    NSLog(@"HNReleaseView:%@",btn.titleLabel.text);
    NSString * _loginName = [NSString stringWithFormat:@"%@",[AppConfig getUserInfoByKey:@"hnLoginName"]];
    HNPublishProductController * publishProduct = [[HNPublishProductController alloc]init];
    if (btn.tag == 20000) {//寄卖回收
        [publishProduct setUrlPath:[NSString stringWithFormat:@"%@?hnLoginName=%@&hnPublishType=0",url_Main_publish,_loginName]];
        //        self.navigationController.navigationBarHidden = YES;
        //       [self.navigationController pushViewController:publishProduct animated:YES];
    }else if(btn.tag == 20001){//闲置
        [publishProduct setUrlPath:[NSString stringWithFormat:@"%@?hnLoginName=%@&hnPublishType=1",url_Main_publish,_loginName]];
    }else if(btn.tag == 20002){
        [publishProduct setUrlPath:[NSString stringWithFormat:@"%@?hnLoginName=%@",url_Main_publish_jimai,_loginName]];
    }else if (btn.tag == 2004){
        NSMutableString *webViewUrl = [NSMutableString stringWithFormat:@"%@?type=%@",url_Different,@"qubie"];
        
        [publishProduct setUrlPath:webViewUrl];
    }
    [self.navigationController pushViewController:publishProduct animated:YES];
}


@end
